<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="用户工号" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入用户工号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="initData"></right-toolbar>
    </el-row>

    <el-table v-loading="table.loading" :data="table.data">
      <el-table-column label="用户工号" align="center" prop="loginName"/>
      <el-table-column label="用户名称" align="center" prop="userName"/>
      <el-table-column label="部门名称" align="center" prop="deptName"/>
      <el-table-column label="IP" align="center" prop="ipAddress"/>
      <el-table-column label="浏览器" align="center" prop="browser"/>
      <el-table-column label="操作系统" align="center" prop="os"/>
      <el-table-column label="登录时间" align="center" prop="loginTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-link
            type="danger"
            icon="el-icon-delete"
            :underline="false"
            @click="handleForceLogout(scope.row)"
            v-hasPermi="['monitor:online:forceLogout']">
            强退
          </el-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="page.total > 0"
      :total="page.total"
      :page.sync="page.current"
      :limit.sync="page.size"
      @pagination="initData"/>
  </div>
</template>

<script>
import {listOnline, forceLogout} from "@/api/monitor/online";

export default {
  name: "Online",
  data() {
    return {
      // 显示搜索条件
      showSearch: true,
      // 状态数据字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        loginName: undefined,
        userName: undefined
      },
      table: {
        data: [],
        loading: false,
      },
      page: {
        total: 0,
        current: 1,
        size: 10,
      }
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      this.initData();
    },
    /** 查询登录日志列表 */
    initData() {
      this.table.loading = true;
      listOnline({
        loginName: this.queryParams.loginName,
        userName: this.queryParams.userName,
        page: true,
        current: this.page.current,
        size: this.page.size
      }).then(response => {
        this.table.data = response.records;
        this.page.total = response.total;
        this.table.loading = false;
      }).catch(() => {
        this.table.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.page.current = 1;
      this.initData();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 强退按钮操作 */
    handleForceLogout(row) {
      this.$confirm('是否确认强退该用户?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return forceLogout(row.tokenId);
      }).then(() => {
        this.handleQuery();
        this.msgSuccess("强退成功");
      }).catch(() => {});
    }
  }
};
</script>

